<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>引用声明</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/reference","wgTitle":"cpp/language/reference","wgCurRevisionId":72309,"wgArticleId":9156,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/reference","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_reference skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">引用声明</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../container.html" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="declarations.html" title="cpp/language/declarations">声明</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> 概览 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations">声明语法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E8.AF.B4.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明说明符序列</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E5.A3.B0.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明符</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 说明符 </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="typedef.html" title="cpp/language/typedef"><span class="t-lines"><span>typedef</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="inline.html" title="cpp/language/inline"><span class="t-lines"><span>inline</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="virtual.html" title="cpp/language/virtual"> virtual 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="explicit.html" title="cpp/language/explicit"> explicit 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="friend.html" title="cpp/language/friend"><span class="t-lines"><span>friend</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constexpr.html" title="cpp/language/constexpr"><span class="t-lines"><span>constexpr</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="consteval.html" title="cpp/language/consteval"><span class="t-lines"><span>consteval</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constinit.html" title="cpp/language/constinit"><span class="t-lines"><span>constinit</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="cpp/language/storage duration"> 存储类说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="tu_local.html" title="cpp/language/tu local">翻译单元局部</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="class.html" title="cpp/language/class"><span class="t-lines"><span>class</span><span>struct</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="union.html" title="cpp/language/union"><span class="t-lines"><span>union</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="enum.html" title="cpp/language/enum"><span class="t-lines"><span>enum</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="decltype.html" title="cpp/language/decltype"><span class="t-lines"><span>decltype</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="auto.html" title="cpp/language/auto"><span class="t-lines"><span>auto</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="alignas.html" title="cpp/language/alignas"><span class="t-lines"><span>alignas</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="cv.html" title="cpp/language/cv"><span class="t-lines"><span>const</span><span>volatile</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="elaborated_type_specifier.html" title="cpp/language/elaborated type specifier">详述类型说明符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="attributes.html" title="cpp/language/attributes"> 属性</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> 声明符 </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 引用</strong> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="pointer.html" title="cpp/language/pointer"> 指针</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="array.html" title="cpp/language/array"> 数组</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 块声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 简单声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="structured_binding.html" title="cpp/language/structured binding">结构化绑定声明</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="type_alias.html" title="cpp/language/type alias"> 别名声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace_alias.html" title="cpp/language/namespace alias"> 命名空间别名定义</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="using_declaration.html" title="cpp/language/using declaration"> using 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html#using_.E6.8C.87.E4.BB.A4" title="cpp/language/namespace"> using 指令</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_assert.html" title="cpp/language/static assert"> static_assert 声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="asm.html" title="cpp/language/asm"> asm 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="enum.html" title="cpp/language/enum"> 不可见 enum 声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 其他声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html" title="cpp/language/namespace"> 命名空间定义</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> 函数声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html" title="cpp/language/class template"> 类模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_template.html" title="cpp/language/function template"> 函数模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html#.E6.98.BE.E5.BC.8F.E5.AE.9E.E4.BE.8B.E5.8C.96" title="cpp/language/class template"> 显式模板实例化</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="template_specialization.html" title="cpp/language/template specialization"> 显式模板特化</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="language_linkage.html" title="cpp/language/language linkage"> 链接说明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 属性声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 空声明</a> </td></tr>
</table></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>声明具名变量为引用，即既存对象或函数的别名。
</p>
<h3><span class="mw-headline" id=".E8.AF.AD.E6.B3.95">语法</span></h3>
<p>引用变量声明是<a href="declarations.html" title="cpp/language/declarations">声明符</a>拥有下列形式的简单声明：
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>&amp;</b></code> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <span class="t-spar">声明符</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>&amp;&amp;</b></code> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <span class="t-spar">声明符</span>
</td>
<td> (2)
</td>
<td> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> <b>左值引用声明符</b>：声明 <span class="t-c"><span class="mw-geshi cpp source-cpp">S<span class="sy3">&amp;</span> D<span class="sy4">;</span></span></span> 将 <code>D</code> 声明为到 <span class="t-spar">声明说明符序列</span> 所确定的类型 <code>S</code> 的<i>左值引用</i>。</div>
<div class="t-li1"><span class="t-li">2)</span> <b>右值引用声明符</b>：声明 <span class="t-c"><span class="mw-geshi cpp source-cpp">S<span class="sy3">&amp;&amp;</span> D<span class="sy4">;</span></span></span> 将 <code>D</code> 声明为到 <span class="t-spar">声明说明符序列</span> 所确定的类型 <code>S</code> 的<i>右值引用</i>。</div>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">声明符</span>
</td>
<td> -
</td>
<td>  除引用<a href="declarations.html" title="cpp/language/declarations">声明符</a>之外的任何其他声明符（不存在引用的引用）
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">属性</span>
</td>
<td> -
</td>
<td>  <span class="t-mark-rev t-since-cxx11">(C++11 起)</span> 可选的<a href="attributes.html" title="cpp/language/attributes">属性</a>列表
</td></tr></table>
<p>引用必须被初始化为指代一个有效的对象或函数：见<a href="reference_initialization.html" title="cpp/language/reference initialization">引用初始化</a>。
</p><p>不存在 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">void</span></span></span> 的引用，也不存在引用的引用。
</p><p>引用类型无法在顶层被 <a href="cv.html" title="cpp/language/cv">cv 限定</a>；声明中没有为此而设的语法，而如果添加限定性到 typedef 名、 <a href="decltype.html" title="cpp/language/decltype"><code>decltype</code></a> 说明符或<a href="template_parameters.html#.E7.B1.BB.E5.9E.8B.E6.A8.A1.E6.9D.BF.E5.BD.A2.E5.8F.82" title="cpp/language/template parameters">类型模板形参</a>，则忽略它。
</p><p>引用不是对象；它们不必占用存储，尽管编译器会在需要实现所需语义（例如，引用类型的非静态数据成员通常会增加类的大小，量为存储内存地址所需）的情况下分配存储。
</p><p>因为引用不是对象，所以不存在引用的数组，不存在指向引用的指针，不存在引用的引用：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span><span class="sy3">&amp;</span> a<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="sy4">;</span> <span class="co1">// 错误</span>
<span class="kw4">int</span><span class="sy3">&amp;</span><span class="sy2">*</span> p<span class="sy4">;</span>   <span class="co1">// 错误</span>
<span class="kw4">int</span><span class="sy3">&amp;</span> <span class="sy3">&amp;</span>r<span class="sy4">;</span>   <span class="co1">// 错误</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<h5> <span class="mw-headline" id=".E5.BC.95.E7.94.A8.E6.8A.98.E5.8F.A0">引用折叠</span></h5>
<p>通过模板或 typedef 中的类型操作可以构成引用的引用，此时适用<i>引用折叠（reference collapsing）</i>规则：右值引用的右值引用折叠成右值引用，所有其他组合均折叠成左值引用：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">typedef</span> <span class="kw4">int</span><span class="sy3">&amp;</span>  lref<span class="sy4">;</span>
<span class="kw1">typedef</span> <span class="kw4">int</span><span class="sy3">&amp;&amp;</span> rref<span class="sy4">;</span>
<span class="kw4">int</span> n<span class="sy4">;</span>
lref<span class="sy3">&amp;</span>  r1 <span class="sy1">=</span> n<span class="sy4">;</span> <span class="co1">// r1 的类型是 int&amp;</span>
lref<span class="sy3">&amp;&amp;</span> r2 <span class="sy1">=</span> n<span class="sy4">;</span> <span class="co1">// r2 的类型是 int&amp;</span>
rref<span class="sy3">&amp;</span>  r3 <span class="sy1">=</span> n<span class="sy4">;</span> <span class="co1">// r3 的类型是 int&amp;</span>
rref<span class="sy3">&amp;&amp;</span> r4 <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// r4 的类型是 int&amp;&amp;</span></pre></div></div>
<p>（这条规则，和将 <code>T&amp;&amp;</code> 用于函数模板时的<a href="template_argument_deduction.html" title="cpp/language/template argument deduction">模板实参推导</a>的特殊规则一起，组成了使得 <span class="t-lc"><a href="../utility/forward.html" title="cpp/utility/forward">std::forward</a></span> 可行的规则。）
</p>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<h4><span class="mw-headline" id=".E5.B7.A6.E5.80.BC.E5.BC.95.E7.94.A8">左值引用</span></h4>
<p>左值引用可用于建立既存对象的别名（可拥有不同的 cv 限定）：
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;string&gt;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> s <span class="sy1">=</span> <span class="st0">"Ex"</span><span class="sy4">;</span>
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> r1 <span class="sy1">=</span> s<span class="sy4">;</span>
    <span class="kw4">const</span> <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> r2 <span class="sy1">=</span> s<span class="sy4">;</span>
 
    r1 <span class="sy2">+</span><span class="sy1">=</span> <span class="st0">"ample"</span><span class="sy4">;</span>           <span class="co1">// 修改 s</span>
<span class="co1">//  r2 += "!";               // 错误：不能通过到 const 的引用修改</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> r2 <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span> <span class="co1">// 打印 s，它现在保有 "Example"</span>
<span class="br0">}</span></pre></div></div>
</div>
<p>它们也能用于在函数调用中实现按引用传递语义：
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;string&gt;</span>
 
<span class="kw4">void</span> double_string<span class="br0">(</span><a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> s<span class="br0">)</span> <span class="br0">{</span>
    s <span class="sy2">+</span><span class="sy1">=</span> s<span class="sy4">;</span> <span class="co1">// 's' 与 main() 的 'str' 是同一对象</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> str <span class="sy1">=</span> <span class="st0">"Test"</span><span class="sy4">;</span>
    double_string<span class="br0">(</span>str<span class="br0">)</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> str <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
</div>
<p>当函数的返回值是左值引用时，函数调用表达式成为<a href="value_category.html" title="cpp/language/value category">左值表达式</a>：
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;string&gt;</span>
 
<span class="kw4">char</span><span class="sy3">&amp;</span> char_number<span class="br0">(</span><a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> s, <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> n<span class="br0">)</span> <span class="br0">{</span>
    <span class="kw1">return</span> s.<span class="me1">at</span><span class="br0">(</span>n<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// string::at() 返回 char 的引用</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> str <span class="sy1">=</span> <span class="st0">"Test"</span><span class="sy4">;</span>
    char_number<span class="br0">(</span>str, <span class="nu0">1</span><span class="br0">)</span> <span class="sy1">=</span> <span class="st0">'a'</span><span class="sy4">;</span> <span class="co1">// 函数调用是左值，可被赋值</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> str <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
</div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<h4> <span class="mw-headline" id=".E5.8F.B3.E5.80.BC.E5.BC.95.E7.94.A8">右值引用</span></h4>
<p>右值引用可用于为临时对象<a href="reference_initialization.html#.E4.B8.B4.E6.97.B6.E9.87.8F.E7.94.9F.E5.AD.98.E6.9C.9F" title="cpp/language/reference initialization">延长生存期</a>（注意，到 const 的左值引用也能延长临时对象生存期，但这些对象无法因此被修改）：
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;string&gt;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> s1 <span class="sy1">=</span> <span class="st0">"Test"</span><span class="sy4">;</span>
<span class="co1">//  std::string&amp;&amp; r1 = s1;           // 错误：不能绑定到左值</span>
 
    <span class="kw4">const</span> <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> r2 <span class="sy1">=</span> s1 <span class="sy2">+</span> s1<span class="sy4">;</span> <span class="co1">// okay：到 const 的左值引用延长生存期</span>
<span class="co1">//  r2 += "Test";                    // 错误：不能通过到 const 的引用修改</span>
 
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;&amp;</span> r3 <span class="sy1">=</span> s1 <span class="sy2">+</span> s1<span class="sy4">;</span>      <span class="co1">// okay：右值引用延长生存期</span>
    r3 <span class="sy2">+</span><span class="sy1">=</span> <span class="st0">"Test"</span><span class="sy4">;</span>                    <span class="co1">// okay：能通过到非 const 的引用修改</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> r3 <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
</div>
<p>更重要的是，当函数同时具有右值引用和左值引用的<a href="overload_resolution.html" title="cpp/language/overload resolution">重载</a>时，右值引用重载绑定到右值（包含纯右值和亡值），而左值引用重载绑定到左值：
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;utility&gt;</span>
 
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span><span class="sy3">&amp;</span> x<span class="br0">)</span> <span class="br0">{</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"左值引用重载 f("</span> <span class="sy1">&lt;&lt;</span> x <span class="sy1">&lt;&lt;</span> <span class="st0">")<span class="es1">\n</span>"</span><span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">int</span><span class="sy3">&amp;</span> x<span class="br0">)</span> <span class="br0">{</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"到 const 的左值引用重载 f("</span> <span class="sy1">&lt;&lt;</span> x <span class="sy1">&lt;&lt;</span> <span class="st0">")<span class="es1">\n</span>"</span><span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span><span class="sy3">&amp;&amp;</span> x<span class="br0">)</span> <span class="br0">{</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"右值引用重载 f("</span> <span class="sy1">&lt;&lt;</span> x <span class="sy1">&lt;&lt;</span> <span class="st0">")<span class="es1">\n</span>"</span><span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
    <span class="kw4">const</span> <span class="kw4">int</span> ci <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>
    f<span class="br0">(</span>i<span class="br0">)</span><span class="sy4">;</span>  <span class="co1">// 调用 f(int&amp;)</span>
    f<span class="br0">(</span>ci<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 调用 f(const int&amp;)</span>
    f<span class="br0">(</span><span class="nu0">3</span><span class="br0">)</span><span class="sy4">;</span>  <span class="co1">// 调用 f(int&amp;&amp;)，如果没有 f(int&amp;&amp;) 重载则会调用 f(const int&amp;)</span>
    f<span class="br0">(</span>std<span class="sy4">::</span><span class="me2">move</span><span class="br0">(</span>i<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 调用 f(int&amp;&amp;)</span>
 
    <span class="co1">// 右值引用变量在用于表达式时是左值</span>
    <span class="kw4">int</span><span class="sy3">&amp;&amp;</span> x <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
    f<span class="br0">(</span>x<span class="br0">)</span><span class="sy4">;</span>            <span class="co1">// 调用 f(int&amp; x)</span>
    f<span class="br0">(</span>std<span class="sy4">::</span><span class="me2">move</span><span class="br0">(</span>x<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 调用 f(int&amp;&amp; x)</span>
<span class="br0">}</span></pre></div></div>
</div>
<p>这允许在适当时机自动选择<a href="move_constructor.html" title="cpp/language/move constructor">移动构造函数</a>、<a href="move_assignment.html" title="cpp/language/move assignment">移动赋值</a>运算符和其他具有移动能力的函数（例如 <span class="t-lc"><a href="../container/vector/push_back.html" title="cpp/container/vector/push back">std::vector::push_back()</a></span>）。
</p><p>因为右值引用能绑定到亡值，所以它们能指代非临时对象：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> i2 <span class="sy1">=</span> <span class="nu0">42</span><span class="sy4">;</span>
<span class="kw4">int</span><span class="sy3">&amp;&amp;</span> rri <span class="sy1">=</span> std<span class="sy4">::</span><span class="me2">move</span><span class="br0">(</span>i2<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 直接绑定到 i2</span></pre></div></div>
<p>这使得作用域中不再需要的对象可以被移动出去：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><a href="../container/vector.html"><span class="kw1269">std::<span class="me2">vector</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> v<span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span>,<span class="nu0">4</span>,<span class="nu0">5</span><span class="br0">}</span><span class="sy4">;</span>
<a href="../container/vector.html"><span class="kw1269">std::<span class="me2">vector</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> v2<span class="br0">(</span>std<span class="sy4">::</span><span class="me2">move</span><span class="br0">(</span>v<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 绑定右值引用到 v</span>
<a href="../error/assert.html"><span class="kw774">assert</span></a><span class="br0">(</span>v.<span class="me1">empty</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<h4> <span class="mw-headline" id=".E8.BD.AC.E5.8F.91.E5.BC.95.E7.94.A8">转发引用</span></h4>
<p>转发引用是一种特殊的引用，它保持函数实参的值类别，使得 <span class="t-lc"><a href="../utility/forward.html" title="cpp/utility/forward">std::forward</a></span> 能用来<i>转发</i>实参。转发引用是下列之一：
</p>
<div class="t-li1"><span class="t-li">1)</span> 函数模板的函数形参，其被声明为同一函数模板的<a href="template_parameters.html#.E7.B1.BB.E5.9E.8B.E6.A8.A1.E6.9D.BF.E5.BD.A2.E5.8F.82" title="cpp/language/template parameters">类型模板形参</a>的无 cv 限定的右值引用：
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span> T<span class="sy1">&gt;</span>
<span class="kw4">int</span> f<span class="br0">(</span>T<span class="sy3">&amp;&amp;</span> x<span class="br0">)</span> <span class="br0">{</span>                    <span class="co1">// x 是转发引用</span>
    <span class="kw1">return</span> g<span class="br0">(</span><a href="../utility/forward.html"><span class="kw1117">std::<span class="me2">forward</span></span></a><span class="sy1">&lt;</span>T<span class="sy1">&gt;</span><span class="br0">(</span>x<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 从而能被转发</span>
<span class="br0">}</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="kw4">int</span> i<span class="sy4">;</span>
    f<span class="br0">(</span>i<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 实参是左值，调用 f&lt;int&amp;&gt;(int&amp;), std::forward&lt;int&amp;&gt;(x) 是左值</span>
    f<span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 实参是右值，调用 f&lt;int&gt;(int&amp;&amp;), std::forward&lt;int&gt;(x) 是右值</span>
<span class="br0">}</span>
 
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span> T<span class="sy1">&gt;</span>
<span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">const</span> T<span class="sy3">&amp;&amp;</span> x<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// x 不是转发引用：const T 不是无 cv 限定的</span>
 
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span> T<span class="sy1">&gt;</span> <span class="kw1">struct</span> A <span class="br0">{</span>
    <span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span> U<span class="sy1">&gt;</span>
    A<span class="br0">(</span>T<span class="sy3">&amp;&amp;</span> x, U<span class="sy3">&amp;&amp;</span> y, <span class="kw4">int</span><span class="sy2">*</span> p<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// x 不是转发引用：T 不是构造函数的类型模板形参</span>
                             <span class="co1">// 但 y 是转发引用</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">2)</span> <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">auto</span><span class="sy3">&amp;&amp;</span></span></span>，但当其从花括号包围的初始化器列表推导时除外：</div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">auto</span><span class="sy3">&amp;&amp;</span> vec <span class="sy1">=</span> foo<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>       <span class="co1">// foo() 可以是左值或右值，vec 是转发引用</span>
<span class="kw4">auto</span> i <span class="sy1">=</span> <a href="../iterator/begin.html"><span class="kw676">std::<span class="me2">begin</span></span></a><span class="br0">(</span>vec<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 也可以</span>
<span class="br0">(</span><span class="sy2">*</span>i<span class="br0">)</span><span class="sy2">++</span><span class="sy4">;</span>                   <span class="co1">// 也可以</span>
g<span class="br0">(</span><a href="../utility/forward.html"><span class="kw1117">std::<span class="me2">forward</span></span></a><span class="sy1">&lt;</span>decltype<span class="br0">(</span>vec<span class="br0">)</span><span class="sy1">&gt;</span><span class="br0">(</span>vec<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 转发，保持值类别</span>
 
<span class="kw1">for</span> <span class="br0">(</span><span class="kw4">auto</span><span class="sy3">&amp;&amp;</span> x<span class="sy4">:</span> f<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
  <span class="co1">// x 是转发引用；这是使用范围 for 循环最安全的方式</span>
<span class="br0">}</span>
 
<span class="kw4">auto</span><span class="sy3">&amp;&amp;</span> z <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// *不是*转发引用（初始化器列表的特殊情形）</span></pre></div></div>
<p>参阅<a href="template_argument_deduction.html#.E4.BB.8E.E5.87.BD.E6.95.B0.E8.B0.83.E7.94.A8.E6.8E.A8.E5.AF.BC" title="cpp/language/template argument deduction">模板实参推导</a>和 <span class="t-lc"><a href="../utility/forward.html" title="cpp/utility/forward">std::forward</a></span>。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<h4><span class="mw-headline" id=".E6.82.AC.E5.9E.82.E5.BC.95.E7.94.A8">悬垂引用</span></h4>
<p>尽管引用一旦初始化就始终指代一个有效的对象或函数，但有可能创建一个程序，其中被指代对象的<a href="lifetime.html" title="cpp/language/lifetime">生存期</a>结束而引用仍保持可访问（<i>悬垂（dangling）</i>）。访问这种引用是未定义行为。
一个常见例子是返回自动变量的引用的函数：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> f<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> s <span class="sy1">=</span> <span class="st0">"Example"</span><span class="sy4">;</span>
    <span class="kw1">return</span> s<span class="sy4">;</span> <span class="co1">// 退出 s 的作用域：调用其析构函数并解分配其存储</span>
<span class="br0">}</span>
 
<a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy3">&amp;</span> r <span class="sy1">=</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 悬垂引用</span>
<a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> r<span class="sy4">;</span>       <span class="co1">// 未定义行为：从悬垂引用读取</span>
<a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> s <span class="sy1">=</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>  <span class="co1">// 未定义行为：从悬垂引用复制初始化</span></pre></div></div>
<p>注意，右值引用和到 const 的左值引用能延长临时对象的生存期（参阅<a href="reference_initialization.html#.E4.B8.B4.E6.97.B6.E9.87.8F.E7.94.9F.E5.AD.98.E6.9C.9F" title="cpp/language/reference initialization">引用初始化</a>中的规则和例外情况）。
</p><p>如果被指代对象被销毁（例如通过显式的析构函数调用），但存储尚未被解分配，则到生存期外的对象的引用仍能以有限的方式使用，且当在同一存储中重新创建对象时也可以变为有效（细节见<a href="lifetime.html#.E5.9C.A8.E7.94.9F.E5.AD.98.E6.9C.9F.E4.B9.8B.E5.A4.96.E8.BF.9B.E8.A1.8C.E8.AE.BF.E9.97.AE" title="cpp/language/lifetime">在生存期之外进行访问</a>）。
</p>
<!-- 
NewPP limit report
Preprocessor visited node count: 2659/1000000
Preprocessor generated node count: 6105/1000000
Post‐expand include size: 79277/2097152 bytes
Template argument size: 23712/2097152 bytes
Highest expansion depth: 15/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:9156-0!*!0!!zh!*!zh!* and timestamp 20211022193547 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/reference&amp;oldid=72309">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/reference&amp;oldid=72309</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/reference">Online version</a></li><li>Offline version retrieved 2022-01-01 00:37.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2021年9月5日 (星期日) 20:40。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过21,154次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 1.718 secs. -->
	</body>
<!-- Cached 20211022193547 -->
</html>